import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
  count: 0,
};

//计算属性
const getters = {
  addOrEven(state) {
    return state.count & 1 ? "奇数" : "偶数";
  },
};

const actions = {
  addCount(store) {
    // console.log(store);
    store.commit("ADDCOUNT");
  },
  dleCount({ commit }) {
    commit("DLECOUNT");
  },
  adda({ commit, state }) {
    if (state.count & 1) {
      commit("ADDA");
    }
  },
  addTime({ commit }) {
    setTimeout(function() {
      //这些方法都可以接收一个参数
      commit("ADDTIME");
    }, 1000);
  },
};

const mutations = {
  ADDCOUNT(state) {
    state.count++;
  },
  DLECOUNT(state) {
    state.count--;
  },
  ADDA(state) {
    state.count++;
  },
  ADDTIME(state) {
    state.count++;
  },
};

const store = new Vuex.Store({
  state,
  getters,
  actions,
  mutations,
});

export default store;
